
#include <iostream>

#include "com_ctrl.h"

using namespace std;

Com_Ctrl::Com_Ctrl(Com_Type type, void *param, uint8_t len)
{
    cout<<"Com_Ctrl"<<endl;

    this->type = type;
    uint32_t *tmp = (uint32_t *)param;
    len = ((len > COM_PARAM_MAX)?COM_PARAM_MAX:len);
    if(COM_USBCAN == type) {
        if(len != (sizeof(USBCAN_Ctrl_Param)/sizeof(uint32_t))) {
            cout << "COM_USBCAN param len error!" << endl;
            return ;   
        }
    } else {
        ;
    }

    for(uint8_t i = 0; i < len; i++) {
        this->param[i] = tmp[i];
    }
}

Com_Ctrl::~Com_Ctrl()
{
    std::cout<<"~Com_Ctrl"<<std::endl;
}


void *com_receive_thread(void *param)
{
    Com_Ctrl *com_ctrl = (Com_Ctrl *)param;

    com_ctrl->com_receive_loop();

    cout << "com_receive thread exit" << endl;
    return NULL;
}

int32_t Com_Ctrl::com_ctrl_init()
{
    //创建接收线程 
    pthread_create(&pid, NULL, com_receive_thread, this);
    this->ready = 1;

    return 0;
}

int32_t Com_Ctrl::com_ctrl_deinit()
{
    if(this->ready) {
        this->com_thread_disable();
        pthread_join(pid, NULL);    //等待线程关闭
    }

    this->ready = 0;
    return 0;
}